package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        dishDTO.setStatus(1);
        Dish dish = Dish.builder()
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .description(dishDTO.getDescription())
                .status(dishDTO.getStatus())
                .build();
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && !flavors.isEmpty()){
            flavors.forEach(flavor->{flavor.setDishId(dish.getId());});
        }
        dishMapper.insertBatch(flavors);
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO>p=dishMapper.page(dishPageQueryDTO);
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        DishVO dishVo=dishMapper.getById(id);
        List<DishFlavor>flavors=dishMapper.geByIdWithFlavor(id);
        dishVo.setFlavors(flavors);
        return dishVo;
    }


    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = Dish.builder()
                .id(dishDTO.getId())
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .description(dishDTO.getDescription())
                .status(dishDTO.getStatus())
                .build();
                dishMapper.update(dish);
          dishMapper.deleteFlavorByDishId(dishDTO.getId());
          List<DishFlavor> flavors = dishDTO.getFlavors();
          if(flavors!=null && !flavors.isEmpty()){
              flavors.forEach(flavor->flavor.setDishId(dish.getId()));
              dishMapper.insertBatch(flavors);
          }
    }

    @Override
    public void delete(List<Long> ids) {
        dishMapper.deleteByIds(ids);
        ids.forEach(id->dishMapper.deleteFlavorByDishId(id));
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.geByIdWithFlavor(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public List<Dish> getDishByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.getDishByCategoryId(categoryId);
        return dishes;
    }


}
